<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../inc/js.jsp"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>菜单管理</title>
    <jsp:include page="../inc/css.jsp"/>

    <style>
        input[type=checkbox], input[type=radio] {
            transform: translateY(3px);
        }
    </style>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <jsp:include page="../inc/menu.jsp"/>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card" style="margin-top: 10px">
                        <!-- /.card-header -->
                        <div class="card-header">
                            <button type="button" title="新增菜单" class="btn btn-success" onclick="_menuList.fn.addInfo()">新增菜单</button>

                            <div class="form-inline" style="margin-top: 10px;">
                                <div class="form-group">
                                    <input id="searchKeyWord" placeholder="名称或编号" class="form-control" style="margin-right: 10px;"/>
                                </div>

                                <button type="button" title="查询" class="btn btn-info" onclick="_menuList.fn.reloadData()"> 查询</button>
                                <button type="button" title="重置搜索条件并查询" class="btn btn-success" style="margin-left: 10px;" onclick="_menuList.fn.resetAndReloadData()"> 重置</button>
                                <button type="button" title="批量删除" class="btn btn-danger" style="margin-left: 10px;" onclick="_menuList.fn.batchDelModal()"> 批量删除</button>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables" style="width: 99%">
                                    <colgroup>
                                        <col class="gradeA even" style="width: 3%"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th><input type="checkbox" onclick="$tutu.checkAll(this)" class="checkall"/></th>
                                        <th style="min-width: 80px;">菜单名称</th>
                                        <th style="min-width: 130px;">跳转路径</th>
                                        <th style="min-width: 160px;">菜单别名</th>
                                        <th style="min-width: 160px;">菜单图标</th>
                                        <th style="width: 20px;">排序</th>
                                        <th style="width: 80px">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
</div>
<!-- ./wrapper -->

<div class="modal inModal fade" id="menuModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">菜单详情</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <form role="form" class="form-group">
                    <input type="hidden" id="menuId"/>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">菜单名称：</label>

                        <div class="col-9">
                            <input type="text" id="menuName" maxlength="40" class="form-control" style="width: 95%"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">菜单别名：</label>

                        <div class="col-9">
                            <input type="text" id="menuLiName" maxlength="30" class="form-control" style="width: 95%"/>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">菜单图标：</label>

                        <div class="col-9">
                            <input type="text" id="menuDescription" maxlength="30" class="form-control" style="width: 55%"/>
                            <a href="https://www.runoob.com/font-awesome/fontawesome-icons-webapp.html" target="_blank" title="点击跳转到图标选择界面" style="cursor: pointer;margin-left: 8px;">选择图标</a>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">排列序号：</label>

                        <div class="col-9">
                            <input type="number" id="menuIndex" maxlength="3" class="form-control" style="width: 95%"/>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" onclick="_menuList.fn.saveInfoPlus()">保存并添加</button>
                <button type="button" class="btn btn-primary" onclick="_menuList.fn.saveInfo(0)">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="delModal" delModal="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="display: inline">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">删除菜单警告</h4>
            </div>
            <div class="modal-body" style="word-break: break-all;">
                确定要删除以下菜单？
                <br/><span id="delSpan"></span>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="_menuList.fn.batchDel()">确定</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<!-- page script -->
<script type="text/javascript">
    let _menuList = {
        v: {
            list: [],
            dTable: null,
            ids: '',
            names: ''
        },
        fn: {
            init: function () {
                _menuList.fn.dataTableInit();
            },
            reloadData: function () {
                _menuList.v.list = [];
                _menuList.v.dTable.DataTable().ajax.reload(null, false);
            },
            resetAndReloadData: function () {
                $('#searchTypeList').val('');
                $('#searchKeyWord').val('');

                _menuList.fn.reloadData();
            },
            dataTableInit: function () {
                _menuList.v.dTable = $tutu.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "autoWidth": true,//自动计算宽度
                    "lengthChange": false,//是否允许用户改变表格每页显示的记录数
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/menu/list",
                        "type": "POST"
                    },
                    "language": $tutu.v.dataTableL,
                    "columns": [
                        {"data": "id"},
                        {"data": "name"},
                        {
                            "data": null,
                            render: function (data) {
                                return "backend/" + data.liName.replace("Li", '') + "/index";
                            }
                        },
                        {"data": "liName"},
                        {
                            "data": null,
                            render: function (data) {
                                return "<i class='nav-icon " + data.description + "'></i>";
                            }
                        },
                        {"data": "sortIndex"},
                        {"data": null}
                    ],
                    /*添加小插件*/
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='编辑' class='btn btn-primary btn-sm edit'>" +
                                "<i class='fa fa-edit'></i>" +
                                "</button>" +
                                "&nbsp;&nbsp;" +
                                "<button type='button' title='删除' class='btn btn-danger btn-sm delete'>" +
                                "<i class='fa fa-remove'></i>" +
                                "</button>",
                            "targets": -1
                        }
                    ],
                    /*展示数据格式化*/
                    "createdRow": function (row, data, index) {
                        _menuList.v.list.push(data);
                        $('td', row).eq(0).html("<input type='checkbox' name='checkboxId' value=" + data.id + ">");
                    },
                    /*回调函数*/
                    rowCallback: function (row, data) {
                        $('td', row).last().find(".edit").click(function () {
                            _menuList.fn.editInfo(data);
                        });

                        $('td', row).last().find(".delete").click(function () {
                            _menuList.fn.delInfo(data.id);
                        });
                    },
                    /*带菜单*/
                    "fnServerParams": function (aoData) {
                        _menuList.v.list = [];
                        aoData.type = $('#searchTypeList').val();
                        aoData.keyWord = $('#searchKeyWord').val();
                    },
                    "fnDrawCallback": function (data) {
                        $tutu.uiForm(data);
                    }
                });
            },
            saveInfoPlus: function () {
                _menuList.fn.saveInfo(1);
            },
            saveInfo: function (type) {
                let name = $('#menuName').val();
                let liName = $('#menuLiName').val();
                let description = $('#menuDescription').val();
                let menuIndex = $('#menuIndex').val();

                if (name === '') {
                    info('请输入菜单名称', "error");
                    return;
                }

                if (liName === '') {
                    info('请输入菜单别名', "error");
                    return;
                }

                if (description === '') {
                    info('请输入菜单图标描述', "error");
                    return;
                }

                if (menuIndex === '' || Number(menuIndex) <= 0 || Number(menuIndex) > 100) {
                    info('排列序号格式错误', "error");
                    return;
                }

                startLoading();

                $.post('backend/menu/save', {
                    id: $('#menuId').val(),
                    name: name,
                    description: description,
                    liName: liName,
                    sortIndex: menuIndex
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("菜单信息已保存", "success");

                        if (type === 0) {
                            $("#menuModal").modal("hide");
                        } else {
                            $('#menuId').val('');
                        }

                        _menuList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            editInfo: function (data) {
                $('#menuId').val(data.id);
                $('#menuName').val(data.name);
                $('#menuLiName').val(data.liName);
                $('#menuDescription').val(data.description);
                $('#menuIndex').val(data.sortIndex);

                $("#menuModal").modal("show");
            },
            addInfo: function () {
                $('#menuId').val('');
                $('#menuName').val('');
                $('#menuLiName').val('');
                $('#menuDescription').val('');
                $('#menuIndex').val(100);

                $("#menuModal").modal("show");
            },
            delInfo: function (id) {
                swal({
                    title: "您确定要删除这条记录吗",
                    text: "删除后将无法恢复，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的，删除！",
                    cancelButtonText: "不，取消",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }, function (isConfirm) {
                    if (isConfirm) {
                        startLoading();

                        $.post('backend/menu/del', {
                            id: id
                        }, function (result) {
                            endLoading();

                            if (result.code === 0) {
                                swal("删除成功！", "您已经删除了这条记录。", "success");

                                _menuList.fn.reloadData();
                            } else {
                                info(result.msg, "error");
                            }
                        });
                    } else {
                        swal("已取消", "您取消了删除操作！", "error");
                    }
                })
            },
            batchDelModal: function () {
                let ids = "";
                let names = "";

                if ($('input:checkbox[name=checkboxId]:checked').length === 0) {
                    info("请先选择至少一项", "error");
                } else {
                    $('input:checkbox[name=checkboxId]:checked').each(function (i) {
                        if (0 === i) {
                            ids = $(this).val();
                            names = $(this).parent().next().next().html();
                        } else {
                            ids += ("," + $(this).val());
                            names += ("," + $(this).parent().next().next().html());
                        }
                    });

                    _menuList.v.ids = ids;
                    $('#delSpan').html(names);

                    $('#delModal').modal('show');
                }
            },
            batchDel: function () {
                startLoading();

                $.post('backend/menu/batchDel', {
                    ids: _menuList.v.ids
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        $('#delModal').modal('hide');
                        swal("删除成功！", "您已经删除了这些菜单", "success");

                        setTimeout(function () {
                            _menuList.fn.reloadData();
                        }, 1000);
                    } else {
                        info(result.msg, "error");
                    }
                });
            }
        }
    };

    $(document).ready(function () {
        _menuList.fn.init();

        window.onload = function () {
            checkSelectMenu("menuLi");

            loadMenuColor('menuLi');
        }
    });
</script>
</body>
</html>
